package dba.example.entities;

import javax.persistence.Column
import javax.persistence.MappedSuperclass
import javax.persistence.PrePersist
import javax.persistence.PreUpdate
import org.joda.time.DateTime
import scala.beans.BeanProperty

/**
 *
 * @author david.arkell
 *
 */
@MappedSuperclass
abstract class AbstractEntity {

  // I initially had these marked as @NotNull but it seems that hibernate
  // validation is not smart enough to figure that it should be caught on an
  // update.
  //@JsonView({ classOf[Views.Detailed] })
  @Column(updatable = false)
  @BeanProperty
  var created: DateTime = null;

  //@JsonView({ Views.Detailed.class })
  @BeanProperty
  var lastUpdated: DateTime = null;

  /**
   * This is automatically called before the record is inserted
   */
  @PrePersist
  private def onCreate() {
    created = new DateTime();
    lastUpdated = new DateTime();
  }

  /**
   * This is automatically called before the record is updated
   */
  @PreUpdate
  private def onUpdate() {
    lastUpdated = new DateTime();
  }

}
